import time
from selenium.webdriver.common.by import By
import allure
from wordpress_project.base.logger import Logger
import os
file_name = time.strftime('%Y_%m_%d')
# 拿到 父级的父级  ， wordpress_project
file = os.path.dirname(os.path.dirname(__file__))
log_path = f'{file}/logs/{file_name}.log' # 日志存放路径

class Tools:

    def __init__(self,driver):
        # driver: 浏览器，只在你传递进来的浏览器操作
        self.log = Logger(file_name=log_path)
        self.driver = driver
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    def visit(self,url):
        #  访问网页 url: 网页地址
        self.log(f'正在访问{url}该网页')
        self.driver.get(url)
        time.sleep(0.5)

    def by_css(self,css):
        """根据css定位，返回定位的元素"""
        try:
            self.log(f'正在使用css定位，表达式为{css}')
            return self.driver.find_element(By.CSS_SELECTOR, css)
        except BaseException as e:
            self.log(f'!!!!!!定位失败!!!!!!!\n错误原因如下：\n{e}')



    def by_link(self,link):
        """根据Link_Text 定位，返回定位的元素"""
        self.log(f'正在使用文本链接定位，文本链接为{link}')
        return self.driver.find_element(By.PARTIAL_LINK_TEXT, link)

    def quit(self):
        self.driver.quit()

    def img(self):
        title = time.strftime('%Y_%m_%d_%H_%M_%S')
        # 截图、附加在测试报告中
        allure.attach(self.driver.get_screenshot_as_png(),title,allure.attachment_type.PNG)


















